<?php
// $Id: SelectInput.php 27 2010-11-16 12:49:54Z mjohnson60@gmail.com $


/**
 * Enter description here ...
 * 
 * @author markjohnson
 * @version 1.0
 * @copyright Copyright (c) 2010, markjohnson
 * @license http://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
 *
 */
class SelectInput extends FormElement
{
	/**
	 * Enter description here ...
	 * 
	 * @var unknown_type
	 */
	var $multiple = FALSE;

	/**
	 * Enter description here ...
	 * 
	 * @param string $name
	 * @param string $value
	 * @param string $id
	 * @param FormElement $parent
	 * @param array $attributes
	 * @param string $title
	 * @param bool $multiple
	 * @param array $options
	 */
	public function __construct($name, $value = NULL, $id = NULL, $parent = NULL, $attributes = array(), $title = '', $multiple = FALSE, $options = array())
	{
		$this->type = 'select';
		$this->title = $title;
		$this->multiple = $multiple;
		$this->children = $options;
		
		foreach ($this->children as $key => $option)
		{
			if (is_array($option))
			{
				$this->children[$key] = new SelectOptionInput($key, $option['#value'], unique_id($key), $this, (empty($option['#attributes']) ? array() : $option['#attributes']), $option['#title'], (empty($option['#selected']) ? FALSE : TRUE));
			}
		}
		
		if (is_array($value))
		{
			parent::__construct($value, $name);
		}
		else
		{
			parent::__construct(NULL, $name, $value, $id, $parent, $attributes);
		}
	}

	/* 
	 * @see FormElement::setForm()
	 */
	public function setForm($form)
	{
		parent::setForm($form);
	}

	/* 
	 * @see FormElement::_compile()
	 */
	public function _compile()
	{
		return $this->render();
	}

	/* 
	 * @see FormElement::render()
	 */
	public function render()
	{
		$this->addHtmlClasses(array(
			'form-select'
		));
		
		return parent::render('<select name="' . $this->name . ($this->multiple ? '[]' : '') . '" ' . ($this->multiple ? 'multiple="multiple"' : '') . (empty($this->attributes) ? '' : attributes($this->attributes)) . ' id="' . $this->id . '">' . $this->renderSelectOptions() . '</select>');
	}

	/**
	 * renderSelectOptions()
	 * 
	 * Enter description here ...
	 * 
	 * @return Ambigous <string, the, mixed, >
	 */
	public function renderSelectOptions()
	{
		return $this->content;
	}
}
